import os
from google.appengine.ext.webapp import template
import cgi
from model import Product
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class ListProduct(webapp.RequestHandler):
	def get(self):
	 	#self.response.headers['Content-Type'] = 'text/plain'
    		#self.response.out.write('Hello, webapp World!')
    		
    		#get product
    		product_query = Product.all()
		products = product_query.fetch(10)

		template_values = {
		      'products': products
              		}
    		#show them
    		    		
		path = os.path.join(os.path.dirname(__file__), 'addproduct.html')
    		self.response.out.write(template.render(path, template_values))
    		#self.redirect('/')

class AddProduct(webapp.RequestHandler):
	def post(self):
		product = Product()

		product.name = self.request.get('name')
		product.description = self.request.get('description')
		product.url = self.request.get('url')
		
		product.put()
		self.redirect('/product/list')
		#self.response.headers['Content-Type'] = 'text/plain'
		#self.response.out.write('Product = ' + product.name)
    		
	
		
		
application = webapp.WSGIApplication(
                                     [('/product/list', ListProduct),
                                      ('/product/add', AddProduct)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()		
